package com.banmao.algorithm.No301To400.No429;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

public class Solution1 {

    public List<List<Integer>> levelOrder(Node root) {

        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }

        ArrayDeque<Node> queue = new ArrayDeque<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();

            ArrayList<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node poll = queue.poll();
                temp.add(poll.val);
                List<Node> children = poll.children;
                if(children != null && children.size() > 0) {
                    queue.addAll(children);
                }
            }
            result.add(temp);
        }
        return result;

    }

}

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
